package cn.mifengkong.appcommon.locate;

import android.app.Activity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.mifengkong.frtools.util.FRString;

import java.util.HashMap;


/**
 * Created by jiangyongxing on 2017/12/18.
 * 描述：
 */

public abstract class FRLocationInfoHelper implements BDLocationListener {

    private final Activity activity;

    public LocationService mLocationService;
    protected double mLatitude;
    protected double mLongitude;
    private LocationListener locationListener;

    public FRLocationInfoHelper(Activity activity, LocationService locationService) {
        this.activity = activity;
        this.mLocationService = locationService;
    }


    public void initLocation() {
        //获取locationservice实例，建议应用中只初始化1个location实例，然后使用，可以参考其他示例的activity，都是通过此种方式获取locationservice实例的
        mLocationService.registerListener(this);
        //注册监听
        int type = activity.getIntent().getIntExtra("from", 0);
        if (type == 0) {
            mLocationService.setLocationOption(mLocationService.getDefaultLocationClientOption());
        } else if (type == 1) {
            mLocationService.setLocationOption(mLocationService.getOption());
        }

    }

    public void startLocate() {
        if (mLocationService != null)
            mLocationService.start();
    }

    public void stopLocate() {
        if (mLocationService != null)
            mLocationService.stop();
    }

    public void onDestroy() {
        if (mLocationService != null) {
            mLocationService.unregisterListener(this);
            stopLocate();
        }
    }

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        mLatitude = bdLocation.getLatitude();
        mLongitude = bdLocation.getLongitude();
        if (locationListener != null) {//如果定义了自己的回调方法那就不走默认的发送定位消息给后台的接口
            locationListener.onReceiveLocation(bdLocation);
            return;
        }
//        FRSharedPreferences.setStringNotClear(SPConstant.CITY_NAME_USER_SELECTED, bdLocation.getCity());
//        CommonUtil.saveLocationCityInfo(bdLocation.getProvince(), bdLocation.getCity(), bdLocation.getDistrict());
        saveLocationInfoToLocal(bdLocation);
        //经纬度   需要提交到服务器
        stopLocate();
    }

//    @Override
//    public void onConnectHotSpotMessage(String s, int i) {
//
//    }

    /**
     * 发送定位消息
     *
     * @param lat 经纬度
     * @param lng
     */
    public void sendLocation(double lat, double lng) {
        if (4.9E-324 == lat || 4.9E-324 == lng || lat == 0 || lng == 0) {
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("lat", String.valueOf(lat));
        map.put("lng", String.valueOf(lng));
//        SaveDataWheneActivityFinishedIntentService.startForSaveLocationInfo(activity, map);
        sendLocateInfoToServer(activity, map);
    }

    /**
     * 发送定位信息给服务器端
     *
     * @param activity
     * @param map
     */
    protected abstract void sendLocateInfoToServer(Activity activity, HashMap<String, String> map);

    /**
     * 保存定位信息到本地
     *
     * @param bdLocation
     */
    protected abstract void saveLocationInfoToLocal(BDLocation bdLocation);

    public void sendLocation() {
        sendLocation(mLatitude, mLongitude);
    }

    public interface LocationListener {
        void onReceiveLocation(BDLocation bdLocation);
    }

    public void setLocationListener(LocationListener locationListener) {
        this.locationListener = locationListener;
    }

    /**
     * 获取经纬度
     *
     * @return
     */
    public String getLatitude() {
        return FRString.valueOf(mLatitude);
    }

    /**
     * 获取经纬度
     *
     * @return
     */
    public String getLongitude() {
        return FRString.valueOf(mLongitude);
    }
}
